package com.qqtech.qquan.homemaking.service;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.Lists;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.homemaking.dao.HomemakingTypeDao;
import com.qqtech.qquan.homemaking.enums.HomemakingTypeStatusDictEnum;
import com.qqtech.qquan.homemaking.model.HomemakingType;
import com.qqtech.qquan.homemaking.model.HomemakingTypeQuery;
import com.qqtech.qquan.homemaking.model.HomemakingTypeVo;

@Service
public class HomemakingTypeServiceImpl extends BaseServiceImpl<HomemakingType>
		implements HomemakingTypeService {
	public static final Logger log = LoggerFactory.getLogger(HomemakingTypeServiceImpl.class);
	
	@Resource
	private HomemakingTypeDao homemakingTypeDao;
	
	private LoadingCache<Integer, List<HomemakingTypeVo>> caches = CacheBuilder
			.newBuilder().recordStats().expireAfterWrite(30, TimeUnit.MINUTES)
			.build(new CacheLoader<Integer, List<HomemakingTypeVo>>() {
				public List<HomemakingTypeVo> load(Integer parentId) {
					HomemakingTypeQuery q = new HomemakingTypeQuery();
					q.setParentId(parentId);
					q.setStatus(HomemakingTypeStatusDictEnum.VALID.getCode());
					Pageable pageKit = new PageKit(1, 100, new PageOrder(
							Direction.ASC, "oidx"));
					List<HomemakingTypeVo> homemakingTypeVoList = homemakingTypeDao
							.queryList(q, pageKit);
					if (homemakingTypeVoList != null) {
						return homemakingTypeVoList;
					}
					return Lists.newArrayList();
				}
			});

	@Override
	protected BaseDao<HomemakingType> getBaseDao() {
		return homemakingTypeDao;
	}
	
	@Override
	public HomemakingTypeVo getByName(java.lang.String name) {
		return homemakingTypeDao.getByName(name);
	}

	@Override
	public List<HomemakingTypeVo> findParent(Integer status) {
		HomemakingTypeQuery q = new HomemakingTypeQuery();
		q.setParentId(0);
		q.setStatus(status);
		Pageable pageKit = new PageKit(1, 9999, new PageOrder(Direction.ASC, "oidx"));
		return homemakingTypeDao.queryList(q,pageKit);
	}

	@Override
	public List<HomemakingTypeVo> findChildren(Integer parentId, Integer status) {
		HomemakingTypeQuery q = new HomemakingTypeQuery();
		q.setParentId(parentId);
		q.setStatus(status);
		Pageable pageKit = new PageKit(1, 9999, new PageOrder(Direction.ASC, "oidx"));
		return homemakingTypeDao.queryList(q,pageKit);
	}

	@Override
	public Page<HomemakingTypeVo> queryPageListAdmin(HomemakingType query,
			Pageable pageable) {
		return homemakingTypeDao.queryPageListAdmin(query, pageable);
	}

	@Override
	public List<HomemakingTypeVo> findChildrenByCache(Integer parentId) {
		if (parentId == null) {
			return Lists.newArrayList();
		}
		return caches.getUnchecked(parentId);
	}	
}